package com.glela.api.upload.qiniu.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glela.api.upload.util.UpLoadConfig;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class NewUploadObject extends UploadObject {
	
	private static final Logger logger = LoggerFactory.getLogger(NewUploadObject.class);
	
	/**
	 * @param id
	 *            七牛视频相关详细信息
	 * @throws Exception
	 * @throws HttpException
	 * @return
	 */
	@Override
	public Map<String, Object> getVideoInfoHttpMessage1(String videoPath) throws HttpException, Exception
	{

		OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(UpLoadConfig.httpBase+videoPath+"?avinfo")
                .addHeader("Content-Type", "application/x-www-form-urlecnoded")
                .build();
        Map<String, Object> resMap=new HashMap<String, Object>();
        
        try {
            Response response = client.newCall(request).execute();
            System.out.println("StatusCode is " + response.code());
            System.out.println(response.headers().toString());
            //System.out.println(new String(response.body().bytes()));
//            String  avinfo=new String(response.body().bytes());
//            String  avinfo = response.body().string();
            System.out.println(new String(response.body().bytes()));
            
            
//            Object object=SerializeUtil.deserializeToObject(response.body().bytes());
            
        

//		//请求是否成功
//		if (response.isSucced()) {
//			String jsonResult = new String(response.body().bytes()));
				JSONObject obj = JSON.parseObject(new String(response.body().bytes()));
				JSONObject videoInfo = obj.getJSONObject("format");
				//				ItemOne itemOne = (ItemOne) items.get(0);
				
				//获取视频时间
				String videoTime=videoInfo.getString("duration");
				//将时间转换为时分秒
				//获取时间（单位秒）
				Double vsTime=Double.parseDouble(videoTime);
				BigDecimal   b   =   new   BigDecimal(vsTime);  
				vsTime=   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
				Double vmTime= vsTime/60;
				String videoTimeStr="";
				//拼接分钟
				if (vmTime>1) {
					videoTimeStr=videoTimeStr+Math.floor(vmTime);
				}
				if (vmTime<10) {
					videoTimeStr="0"+videoTimeStr;
				}
				//得到秒数
				 vsTime=(vmTime-Math.floor(vmTime))*60;
				 //秒数保留整数，四舍五入
				 vsTime=(double) Math.round(vsTime); 
				 if (vsTime>10) {
					 videoTimeStr=videoTimeStr+":0"+vsTime;
				}else if (vsTime>0&&vsTime<10) {
					 videoTimeStr=videoTimeStr+":"+vsTime;
				}
				
				 //获取double类型的时间保留小数后两位
				 resMap.put("doubleTime", new   BigDecimal( Double.parseDouble(videoTime)).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue()     );
				 resMap.put("FrmatTime", videoTimeStr);
				 
				
				resMap.put("success", true);
        } catch (IOException e) {
            System.out.println(e.toString());
            logger.error("getVideoInfoHttpMessage1异常：", e);
            resMap.put("success", false);
        }
//
//			}
//			else {
//				
//			}

		
//
//		OkHttpClient client = new OkHttpClient();
//
//        Request request1 = new Request.Builder()
//                .url(YtUpLoadConfig.httpBase+videoPath+"?avinfo")
//                .addHeader("Content-Type", "application/x-www-form-urlecnoded")
//                .build();
//        try {
//            Response response1 = client.newCall(request1).execute();
//            System.out.println("StatusCode is " + response.code());
//            System.out.println(response.headers().toString());
//            System.out.println(new String(response.body().bytes()));
//        } catch (IOException e) {
//            System.out.println(e.toString());
//        }
    

		
		
		
		
		
		return resMap;

	}

}
